<?php
/**
 * Created by PhpStorm.
 * User: mhx
 * Date: 17-4-11
 * Time: 下午6:25
 */
//https://zh.wikipedia.org/w/api.php?action=query&titles=关键字&format=jsonfm&prop=revisions&rvprop=content&formatversion=2

class wikiController extends baseController{

    public function run(){




    }


    public function getcontents($keywords){
        if($keywords ==null){return false;}
        $url_perfix = 'zh.wikipedia.org/w/api.php?action=query&titles=';
        $url_postfix = '&format=json&prop=revisions&rvprop=content&formatversion=2';
        $url = $url_perfix.$keywords.$url_postfix;
        echo 2123;
        //$str = file_get_contents($url);

        echo 432423;
        $c = $this->curl($url,'');
        print_r($c);die;
        //echo $url;
        $contents = url_crawler($url);
//        print_r($contents);die;
        $newcon = json_decode($contents);
        echo 123;
        print_r($newcon);die;
        return null;

    }

    private function curl($url, $data, $header = false, $method = "POST")
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if ($header) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        if ($data) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        }
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $ret = curl_exec($ch);
        return $ret;
    }


    protected function geturl($url){

        $header = array (
            //"POST {$path}?{$query} HTTP/1.1",
            //"Host: {$temp['host']}",
            //"Content-Type: text/xml; charset=utf-8",
            'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Encoding: gzip, deflate, br',
            'Accept-Language: zh,en-US;q=0.7,en;q=0.3',
            'Cache-Control: max-age=0',
            'Connection: keep-alive',
            "Cookie: WMF-Last-Access=11-Apr-2017; WMF-Last-Access-Global=11-Apr-2017; TBLkisOn=0; CP=H2; GeoIP=CN:61:Xi\'an
:34.26:108.93:v4",
            'Host: zh.wikipedia.org',
            'Upgrade-Insecure-Requests: 1',
            'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/48.0',

            //"Content-length: 380",
            //"Connection: Close"
        );

        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        //是否要头信息
        curl_setopt($ch,CURLOPT_HEADER,1);

        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  //设置头信息的地方
        //是否要正文
        //curl_setopt($ch, CURLOPT_NOBODY, 0);

        //自动根据返回的location进行重定向，解决3XX问题
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        //得到的数据不直接打印到屏幕上
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $str = curl_exec($ch);
        print_r($str);die;

        /*$obj=simplexml_load_string($str,'SimpleXMLElement',LIBXML_NOCDATA);
        $obj=get_object_vars($obj);
        $revisions = $obj['query']->pages->page->revisions->rev;
        print_r($revisions);die;*/

        //获取头文件大小并且截取那么大的数据，得到头信息，其实可以直接get_headers()，不同之处在于后者得请求两次同一个url
        //$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
        //$header = mb_substr($contents,0,$header_size);
        //获取头文件的 content_type
        //$content_type = curl_getinfo($ch,CURLINFO_CONTENT_TYPE);
        curl_close($ch);
        //echo $contents;die;
        //$contents = iconv($charset,'UTF-8//IGNORE',$contents);
        echo 456;

        echo 333;
        print_r($contents);die;
        return $contents;
    }


}